//
//  GBShowPhotoViewController.m
//  PublicSentimentMonitorSystem
//
//  Created by 郭斌 on 15/8/10.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "GBShowPhotoViewController.h"
#import "VIPhotoView.h"

@interface GBShowPhotoViewController ()<UIScrollViewDelegate>
{
    UIScrollView *scrollviewP; //添加photo的scroll
    int currentNumber; //当前的页数
    
}

@end

@implementation GBShowPhotoViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        // Custom initialization
        _data = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WQColor(28, 26, 28);
    self.title = @"图片列表";
    
    //设置导航栏为半透明
    self.navigationController.navigationBar.translucent = YES;
    // 隐藏标签栏
    self.tabBarController.tabBar.hidden = YES;
    // 隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    //单击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    
//    //手指捏合放大缩小手势
//    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinView:)];
//    [self.view addGestureRecognizer:pin];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = KDColor;// [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    
    // 添加导航栏退回按钮
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    self.navigationItem.leftBarButtonItem = backItem;
    //设置导航栏颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = KDColor;//[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    
    //创建scrollview
    [self creatImgShow];
    // 设置数据源
    [self setImgSource:_data];
    //滚动到指定的位置去
    [scrollviewP setContentOffset:CGPointMake(self.view.frame.size.width * _index, 0) animated:YES];
    // Do any additional setup after loading the view.
}

#pragma mark -NavAction
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 设置数据源
- (void)setImgSource:(NSMutableArray *)imgSource
{
    //NSLog(@"%lu", (unsigned long)_data.count);
    if (_data != imgSource)
    {
        _data = imgSource;
    }
    scrollviewP.contentSize = CGSizeMake(self.view.frame.size.width * _data.count, self.view.frame.size.height);
    for(int i = 0; i < _data.count; i++)
    {
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
//        // 设置图片显示样式
//        imageView.contentMode = UIViewContentModeScaleAspectFit;
//        imageView.image = _data[i];
//        //手指捏合放大缩小手势
//        UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinView:)];
//        [imageView addGestureRecognizer:pin];
//        //开启用户交互模式
//        imageView.userInteractionEnabled = YES;
//        [scrollviewP addSubview:imageView];
        
        VIPhotoView *photoView = [[VIPhotoView alloc] initWithFrame:CGRectMake(i * screenW, 0, screenW, screenH) andImage:_data[i]];
        photoView.autoresizingMask = (1 << 6) -1;
        [scrollviewP addSubview:photoView];
    }
    //NSLog(@"%lu",(unsigned long)_imgSource.count);
}

//- (void)pinView:(UIPinchGestureRecognizer *)sender
//{
//    NSLog(@"123456");
//    //通过手势的到当前操作的视图
//    NSLog(@"%@",sender.view);
//    UIView *v = sender.view;
//    //实现放大缩小
//    v.transform = CGAffineTransformScale(v.transform, sender.scale, sender.scale);
//    //缩放比例会进行累加,所以每次都应该重置缩放比例
//    sender.scale = 1;
//}

// 初始化视图
- (void)creatImgShow
{
    scrollviewP = [[UIScrollView alloc] initWithFrame:self.view.frame];
    // 设置代理
    scrollviewP.delegate = self;
    // 不显示滚动条
    scrollviewP.showsHorizontalScrollIndicator = NO;
    scrollviewP.showsVerticalScrollIndicator = NO;
    // 设置分页显示
    scrollviewP.pagingEnabled = YES;
    //设置背景颜色
    scrollviewP.backgroundColor = [UIColor clearColor];
    scrollviewP.contentSize = CGSizeMake(self.view.frame.size.width * _data.count, self.view.frame.size.height);
    
    //NSLog(@"%@",_data);
    [self.view addSubview:scrollviewP];
}

#pragma mark -UIGestureReconginzer
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // 隐藏导航栏
    [UIView animateWithDuration:0.3 animations:^{
        self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
    }];
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //当前的页数
    currentNumber = scrollView.contentOffset.x / self.view.frame.size.width;
    //NSLog(@"^^^ %d",currentNumber);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
